package com.zqr.wait_notify;

import java.util.List;

/**
 * @author zqr
 * @since 2022/11/4
 */
// 消费者
public class Consumer01 implements Runnable {
    private List<Integer> list;
    private Integer limit; // 最大的元素个数
    private Integer speed; // 消费速度

    public Consumer01(List<Integer> list, Integer limit, Integer speed) {
        this.list = list;
        this.limit = limit;
        this.speed = speed;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (list) {
                if (list.size() <= 0) {
                    try {
                        System.out.println("容器空了，消费者阻塞，阻塞线程：" + Thread.currentThread().getName());
                        list.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    list.remove(0);
                    System.out.println(Thread.currentThread().getName()+"消费数据,消费数据后当前容器大小:" + list.size());
                    Thread.sleep(speed);
                    // 唤醒生产者
                    list.notifyAll();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
